package com.facebook.imagepipeline.image;

import android.util.Pair;
import com.facebook.common.internal.Preconditions;
import com.facebook.common.internal.Supplier;
import com.facebook.common.internal.VisibleForTesting;
import com.facebook.common.references.CloseableReference;
import com.facebook.common.references.SharedReference;
import com.facebook.imageformat.ImageFormat;
import com.facebook.imageformat.ImageFormatChecker;
import com.facebook.imagepipeline.memory.PooledByteBuffer;
import com.facebook.imagepipeline.memory.PooledByteBufferInputStream;
import com.facebook.imageutils.BitmapUtil;
import com.facebook.imageutils.JfifUtil;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

@Immutable
public class EncodedImage
  implements Closeable
{
  public static final int DEFAULT_SAMPLE_SIZE = 1;
  public static final int UNKNOWN_HEIGHT = -1;
  public static final int UNKNOWN_ROTATION_ANGLE = -1;
  public static final int UNKNOWN_STREAM_SIZE = -1;
  public static final int UNKNOWN_WIDTH = -1;
  private int mHeight = -1;
  private ImageFormat mImageFormat = ImageFormat.UNKNOWN;

  @Nullable
  private final Supplier<FileInputStream> mInputStreamSupplier;

  @Nullable
  private final CloseableReference<PooledByteBuffer> mPooledByteBufferRef;
  private int mRotationAngle = -1;
  private int mSampleSize = 1;
  private int mStreamSize = -1;
  private int mWidth = -1;

  public EncodedImage(Supplier<FileInputStream> paramSupplier)
  {
    Preconditions.checkNotNull(paramSupplier);
    this.mPooledByteBufferRef = null;
    this.mInputStreamSupplier = paramSupplier;
  }

  public EncodedImage(Supplier<FileInputStream> paramSupplier, int paramInt)
  {
    this(paramSupplier);
    this.mStreamSize = paramInt;
  }

  public EncodedImage(CloseableReference<PooledByteBuffer> paramCloseableReference)
  {
    Preconditions.checkArgument(CloseableReference.isValid(paramCloseableReference));
    this.mPooledByteBufferRef = paramCloseableReference.clone();
    this.mInputStreamSupplier = null;
  }

  public static EncodedImage cloneOrNull(EncodedImage paramEncodedImage)
  {
    if (paramEncodedImage != null)
      return paramEncodedImage.cloneOrNull();
    return null;
  }

  public static void closeSafely(@Nullable EncodedImage paramEncodedImage)
  {
    if (paramEncodedImage != null)
      paramEncodedImage.close();
  }

  public static boolean isMetaDataAvailable(EncodedImage paramEncodedImage)
  {
    return (paramEncodedImage.mRotationAngle >= 0) && (paramEncodedImage.mWidth >= 0) && (paramEncodedImage.mHeight >= 0);
  }

  public static boolean isValid(@Nullable EncodedImage paramEncodedImage)
  {
    return (paramEncodedImage != null) && (paramEncodedImage.isValid());
  }

  public EncodedImage cloneOrNull()
  {
    EncodedImage localEncodedImage;
    if (this.mInputStreamSupplier != null)
    {
      localEncodedImage = new EncodedImage(this.mInputStreamSupplier, this.mStreamSize);
      if (localEncodedImage != null)
        localEncodedImage.copyMetaDataFrom(this);
      return localEncodedImage;
    }
    CloseableReference localCloseableReference = CloseableReference.cloneOrNull(this.mPooledByteBufferRef);
    if (localCloseableReference == null)
      localEncodedImage = null;
    while (true)
    {
      CloseableReference.closeSafely(localCloseableReference);
      break;
      try
      {
        localEncodedImage = new EncodedImage(localCloseableReference);
      }
      finally
      {
        CloseableReference.closeSafely(localCloseableReference);
      }
    }
  }

  public void close()
  {
    CloseableReference.closeSafely(this.mPooledByteBufferRef);
  }

  public void copyMetaDataFrom(EncodedImage paramEncodedImage)
  {
    this.mImageFormat = paramEncodedImage.getImageFormat();
    this.mWidth = paramEncodedImage.getWidth();
    this.mHeight = paramEncodedImage.getHeight();
    this.mRotationAngle = paramEncodedImage.getRotationAngle();
    this.mSampleSize = paramEncodedImage.getSampleSize();
    this.mStreamSize = paramEncodedImage.getSize();
  }

  public CloseableReference<PooledByteBuffer> getByteBufferRef()
  {
    return CloseableReference.cloneOrNull(this.mPooledByteBufferRef);
  }

  public int getHeight()
  {
    return this.mHeight;
  }

  public ImageFormat getImageFormat()
  {
    return this.mImageFormat;
  }

  public InputStream getInputStream()
  {
    if (this.mInputStreamSupplier != null)
      return (InputStream)this.mInputStreamSupplier.get();
    CloseableReference localCloseableReference = CloseableReference.cloneOrNull(this.mPooledByteBufferRef);
    if (localCloseableReference != null)
      try
      {
        PooledByteBufferInputStream localPooledByteBufferInputStream = new PooledByteBufferInputStream((PooledByteBuffer)localCloseableReference.get());
        return localPooledByteBufferInputStream;
      }
      finally
      {
        CloseableReference.closeSafely(localCloseableReference);
      }
    return null;
  }

  public int getRotationAngle()
  {
    return this.mRotationAngle;
  }

  public int getSampleSize()
  {
    return this.mSampleSize;
  }

  public int getSize()
  {
    if ((this.mPooledByteBufferRef != null) && (this.mPooledByteBufferRef.get() != null))
      return ((PooledByteBuffer)this.mPooledByteBufferRef.get()).size();
    return this.mStreamSize;
  }

  @VisibleForTesting
  public SharedReference<PooledByteBuffer> getUnderlyingReferenceTestOnly()
  {
    try
    {
      if (this.mPooledByteBufferRef != null)
      {
        SharedReference localSharedReference2 = this.mPooledByteBufferRef.getUnderlyingReferenceTestOnly();
        localSharedReference1 = localSharedReference2;
        return localSharedReference1;
      }
      SharedReference localSharedReference1 = null;
    }
    finally
    {
    }
  }

  public int getWidth()
  {
    return this.mWidth;
  }

  public boolean isCompleteAt(int paramInt)
  {
    if (this.mImageFormat != ImageFormat.JPEG);
    PooledByteBuffer localPooledByteBuffer;
    do
    {
      do
        return true;
      while (this.mInputStreamSupplier != null);
      Preconditions.checkNotNull(this.mPooledByteBufferRef);
      localPooledByteBuffer = (PooledByteBuffer)this.mPooledByteBufferRef.get();
    }
    while ((localPooledByteBuffer.read(paramInt - 2) == -1) && (localPooledByteBuffer.read(paramInt - 1) == -39));
    return false;
  }

  public boolean isValid()
  {
    try
    {
      if (!CloseableReference.isValid(this.mPooledByteBufferRef))
      {
        Supplier localSupplier = this.mInputStreamSupplier;
        if (localSupplier == null);
      }
      else
      {
        bool = true;
        return bool;
      }
      boolean bool = false;
    }
    finally
    {
    }
  }

  public void parseMetaData()
  {
    ImageFormat localImageFormat = ImageFormatChecker.getImageFormat_WrapIOException(getInputStream());
    this.mImageFormat = localImageFormat;
    if (!ImageFormat.isWebpFormat(localImageFormat))
    {
      Pair localPair = BitmapUtil.decodeDimensions(getInputStream());
      if (localPair != null)
      {
        this.mWidth = ((Integer)localPair.first).intValue();
        this.mHeight = ((Integer)localPair.second).intValue();
        if (localImageFormat != ImageFormat.JPEG)
          break label90;
        if (this.mRotationAngle == -1)
          this.mRotationAngle = JfifUtil.getAutoRotateAngleFromOrientation(JfifUtil.getOrientation(getInputStream()));
      }
    }
    return;
    label90: this.mRotationAngle = 0;
  }

  public void setHeight(int paramInt)
  {
    this.mHeight = paramInt;
  }

  public void setImageFormat(ImageFormat paramImageFormat)
  {
    this.mImageFormat = paramImageFormat;
  }

  public void setRotationAngle(int paramInt)
  {
    this.mRotationAngle = paramInt;
  }

  public void setSampleSize(int paramInt)
  {
    this.mSampleSize = paramInt;
  }

  public void setStreamSize(int paramInt)
  {
    this.mStreamSize = paramInt;
  }

  public void setWidth(int paramInt)
  {
    this.mWidth = paramInt;
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.facebook.imagepipeline.image.EncodedImage
 * JD-Core Version:    0.6.2
 */